
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import CookeryRoomStep from "./CookeryRoomStep";
import CollisionHelper from "../../../cfw/collide/CollisionHelper";
import BagManager from "../../bag/BagManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CookeryRoomStep3 extends CookeryRoomStep {

    @property(cc.Node)
    handle: cc.Node = null;


    @property(cc.Node)
    targetPost: cc.Node = null;

    start() {
        this.node.active = true;
        // this.push(x, y, w / 2, h / 2)
        this.handle.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.handle.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.handle.on(cc.Node.EventType.TOUCH_END, this.touchCancel, this)
        this.handle.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this)
    }


    touchCancel() {

        if (CollisionHelper.isRectCollideRect(this.handle.getBoundingBox(), this.targetPost.getBoundingBox())) {
            this.handle.x = this.targetPost.x;
            this.handle.y = this.targetPost.y;
            BagManager.addCoin(10, this.targetPost.parent.convertToWorldSpaceAR(this.targetPost.getPosition()))
            this.finish()
        }

    }

    touchMove(e) {
        let pos = e.getLocation();

        let wpos = this.node.convertToNodeSpaceAR(pos)
        // cc.log(' wpos  ', wpos.x, wpos.y)
        this.handle.x = wpos.x
        this.handle.y = wpos.y;

    }

    touchStart(e: cc.Touch) {


    }



    finish() {
        cc.log(' finish == ')
        GlobalEvent.instance().emit(EventName.UPDATE_COOKIE_STEP, 3)
    }


}
